package exhi.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stp.core.entity.IGridRowFactory;
import com.stp.core.entity.XGrid;
import com.stp.core.entity.XGridPage;
import com.stp.core.entity.XGridRow;
import com.stp.core.util.Identities;
import com.stp.rbac.entity.CapabilityInfo;
import com.stp.rbac.manager.CapabilityMng;

import exhi.entity.EntpBexhi;
import exhi.manager.EntpBexhiMng;

@Controller
@RequestMapping("/gov")
public class EvalExhiAct extends BaseAct {

	private static final String PATH_TABLE = "/gov-eval-table";
	private static final String PATH_LIST = "/exhi-eval-list";
	private static final String PATH_VIEW = "/gov-eval-tab";

	private String code = "";

	@RequestMapping(PATH_TABLE)
	public void table(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			ModelMap model) {
		model.addAttribute("colName", "商务委评估意见");
		model.addAttribute("code", code);
		model.addAttribute("url", PATH_LIST.substring(1));
	}

	// 权限json数据
	@RequestMapping(PATH_LIST)
	@ResponseBody
	public String list(
			XGridPage page,
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@RequestParam("cap_code") String capCode,
			@RequestParam("cap_name") String capName)
			throws InstantiationException, IllegalAccessException {
		this.code=code;
		String sql = "from EntpBexhi info where 1=1  and info.status in ('60','70','90') ";	
		sql += sqlModel
				.getSQLChars("info.zlhmc", "like", capName, false, "and");
		sql += " order by info.createtime desc";
		ExhiGridRow row = new EvalExhiAct.ExhiGridRow();
		XGrid grid = entpBexhiMng.getXGrid(sql, row, page.getPageNo(),
				page.getPageSize(), false);
		if (null == grid)
			return "";
		return "{\"xGrid\":" + toJson(grid) + "}";
	}

	class ExhiGridRow implements IGridRowFactory {
		public XGridRow builderXRow(Object obj) {
			// TODO Auto-generated method stub
			XGridRow xrow = null;
			if (obj instanceof EntpBexhi) {
				xrow = new XGridRow();
				EntpBexhi info = (EntpBexhi) obj;
				xrow.setId(info.getId());
				List t = new ArrayList();
				t.add("");
				t.add(info.getZlhmc());
				t.add(getUrl(info.getId()));
				xrow.setData(t);
			}
			return xrow;
		}
	}

	private String getUrl(String id) {
		String s = "";
		s = "<a target='_blank'  href='gov-exhi-view.jspx?infoId=" + id
				+ "&code=" + code + "'>【展会详情】</a>";
		s += "<a href='" + PATH_VIEW.substring(1) + ".jspx?infoId=" + id
				+ "&code=" + code + "'>【评估意见】</a>";
		return s;
	}

	@RequestMapping(PATH_VIEW)
	public void infoCheck(
			@RequestParam(value = "code", required = true, defaultValue = "") String code,
			@ModelAttribute("entpBexhi") EntpBexhi entpBexhiInfo, ModelMap model) {
		String capId = "";
		model.addAttribute("code", code);
		showView(entpBexhiInfo, model);
		List<CapabilityInfo> lst = capabilityMng.findByProperty(
				"capabilityCode", code);
		for (CapabilityInfo info : lst) {
			capId=info.getId();
		}
		Collection<CapabilityInfo> lstTab = capabilityMng.findByProperty(
				"parentId", capId, "sort");
		model.addAttribute("tabs", lstTab);
		if (lstTab.size() > 0) {
			CapabilityInfo tab = lstTab.iterator().next();
			model.addAttribute("url", tab.getCapabilityParams());
		}
		model.addAttribute("infoId", entpBexhiInfo.getId());
		// 企业评估场馆
		String c2eId = getSurveyId("c2e");
		model.addAttribute("c2eId", c2eId);
		// 场馆评估企业
		String e2cId = getSurveyId("e2c");
		model.addAttribute("e2cId", e2cId);
	
	}

	public void showView(EntpBexhi entpBexhi, ModelMap model) {
		if (null == entpBexhi.getId() || "".equalsIgnoreCase(entpBexhi.getId())) {
			model.addAttribute("projectId", Identities.uuid());
		} else {
			model.addAttribute("projectId", entpBexhi.getId());
		}
		model.addAttribute("info", entpBexhi);
		String zb = "";
		String qt = "";
		String cb = "";
		String zc = "";
		String xb = "";
		String exhiType = "";
		String zldd = "";

		if (entpBexhi.getId() == null) {
			model.addAttribute("projectId", Identities.uuid());
		} else {
			model.addAttribute("projectId", entpBexhi.getId());
			zb = this.getEntpRelaction(entpBexhi.getId(), "ZB");// 主办单位
			qt = this.getEntpRelaction(entpBexhi.getId(), "QT");// 其他单位
			cb = this.getEntpRelaction(entpBexhi.getId(), "CB");// 承办单位
			zc = this.getEntpRelaction(entpBexhi.getId(), "ZC");// 支持单位
			xb = this.getEntpRelaction(entpBexhi.getId(), "XB");// 协办单位

			exhiType = this.getEntpRelaction(entpBexhi.getId(), "exhiType");// 展会类型
			zldd = this.getEntpRelaction(entpBexhi.getId(), "zldd");// 展会场馆

		}
		model.addAttribute("zb", zb);
		model.addAttribute("qt", qt);
		model.addAttribute("cb", cb);
		model.addAttribute("zc", zc);
		model.addAttribute("xb", xb);
		model.addAttribute("exhiType", exhiType);
		model.addAttribute("zldd", zldd);
	}

	@ModelAttribute("entpBexhi")
	public EntpBexhi getEntpSexhiInfo(
			@RequestParam(value = "infoId", required = true, defaultValue = "") String id) {
		EntpBexhi entpBexhi = entpBexhiMng.get(id);
		if (null == entpBexhi) {
			entpBexhi = new EntpBexhi();
		}
		return entpBexhi;
	}

	@Autowired
	@Qualifier("entpBexhiMng")
	private EntpBexhiMng entpBexhiMng;

	@Autowired
	@Qualifier("capabilityMng")
	private CapabilityMng capabilityMng;

}
